<template>
  <div class="admin-applications">
    <h2>管理员申请审核</h2>
    
    <div class="applications-list">
      <div v-if="pendingApplications.length === 0" class="application-item">
        暂无待审核的申请
      </div>
      
      <div v-else v-for="app in pendingApplications" 
           :key="app.username" 
           class="application-item">
        <div class="user-info">
          <div class="username">{{ app.username }}</div>
          <div class="reason">申请理由：{{ app.reason }}</div>
          <div class="time">申请时间：{{ formatDate(app.applyTime) }}</div>
        </div>
        
        <div class="status" :class="app.status">
          {{ app.status === 'pending' ? '待审核' : 
             app.status === 'approved' ? '已通过' : '已拒绝' }}
        </div>
        
        <div class="actions" v-if="app.status === 'pending'">
          <button class="approve" @click="handleApplication(app, 'approved')">
            通过
          </button>
          <button class="reject" @click="handleApplication(app, 'rejected')">
            拒绝
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdminApplicationsComponent',
  data() {
    return {
      applications: []
    }
  },
  computed: {
    pendingApplications() {
      return this.applications.filter(app => app.status === 'pending')
    }
  },
  created() {
    this.loadApplications()
  },
  methods: {
    loadApplications() {
      this.applications = JSON.parse(localStorage.getItem('admin_applications') || '[]')
    },
    formatDate(dateString) {
      return new Date(dateString).toLocaleString()
    },
    async handleApplication(application, status) {
      if (status === 'rejected') {
        const reason = prompt('请输入拒绝理由：')
        if (!reason?.trim()) {
          alert('请填写拒绝理由')
          return
        }
        application.rejectReason = reason.trim()
      }

      // 更新申请状态
      const appIndex = this.applications.findIndex(app => 
        app.username === application.username && app.status === 'pending'
      )
      
      if (appIndex !== -1) {
        this.applications[appIndex].status = status
        this.applications[appIndex].handleTime = new Date().toISOString()
        localStorage.setItem('admin_applications', JSON.stringify(this.applications))

        if (status === 'approved') {
          // 更新用户权限
          const users = JSON.parse(localStorage.getItem('users') || '[]')
          const userIndex = users.findIndex(u => u.username === application.username)
          if (userIndex !== -1) {
            users[userIndex].isAdmin = true
            localStorage.setItem('users', JSON.stringify(users))
            
            // 如果是当前登录用户，也更新currentUser
            const currentUser = JSON.parse(localStorage.getItem('currentUser') || '{}')
            if (currentUser.username === application.username) {
              currentUser.isAdmin = true
              localStorage.setItem('currentUser', JSON.stringify(currentUser))
            }
          }
        }

        alert(status === 'approved' ? '已通过申请' : `已拒绝申请：${application.rejectReason}`)
      }
    }
  }
}
</script>

<style lang="scss">
@import '../style/AdminApplication.scss';
</style> 